﻿using Learun.Util.SqlSugar;
using Learun.Util;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Text;
using DbType = System.Data.DbType;

namespace Learun.Application.TwoDevelopment.ZZDT_EC
{
    /// <summary>
    /// 版 本 PIT-ADMS V7.0.3 敏捷开发框架
    /// Copyright (c) 2013-2018 Hexagon PPM
    /// 创 建：超级管理员
    /// 日 期：2022-07-04 10:00
    /// 描 述：同步关联表
    /// </summary>
    public class ec_synrel_tableService
    {
        #region 仓储
        Repository<ec_synrel_tableEntity> _synrelTableRepository => new Repository<ec_synrel_tableEntity>();
        #endregion
        #region 获取数据

        /// <summary>
        /// 获取列表数据
        /// <summary>
        /// <returns></returns>
        public IEnumerable<ec_synrel_tableEntity> GetList( string queryJson )
        {
            try
            {
                var queryParam = queryJson.ToJObject();
                var strSql = new StringBuilder();
                strSql.Append(@" SELECT t1.*,t2.BusinessTableCode AS MainBusinessTableCode,t3.BusinessTableCode AS RelBusinessTableCode
                        FROM ec_synrel_table t1
                        LEFT JOIN ec_business_table t2 on t1.MainBusinessTableID=t2.BusinessTableID
                        LEFT JOIN ec_business_table t3 on t1.RelBusinessTableID=t3.BusinessTableID
                            ");
                strSql.Append(" WHERE 1=1 ");
                // 虚拟参数
                //var dp = new DynamicParameters(new { });
                var dp = new List<SugarParameter>();
                if (!queryParam["MainBusinessTableID"].IsEmpty())
                {
                    dp.Add(new SugarParameter("MainBusinessTableID", queryParam["MainBusinessTableID"].ToString(), DbType.String));
                    strSql.Append(" AND t1.MainBusinessTableID = @MainBusinessTableID ");
                }
                strSql.Append(" ORDER BY t1.CreateTime DESC ");
                //return this.BaseRepository().FindList<ec_synrel_tableEntity>(strSql.ToString(), dp);
                return SqlSugarHelper.Db.SqlQueryable<ec_synrel_tableEntity>(strSql.ToString()).AddParameters(dp).ToList();
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }

        /// <summary>
        /// 获取实体数据
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public ec_synrel_tableEntity GetEntity(string keyValue)
        {
            try
            {
                return _synrelTableRepository.GetById(keyValue);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }

        #endregion

        #region 提交数据


        /// <summary>
        /// 保存实体数据（新增、修改）
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public void SaveEntity(string mainBusinessTableID, List<ec_synrel_tableEntity> entities)
        {
            SqlSugarHelper.Db.BeginTran();
            try
            {
                _synrelTableRepository.Delete(t => t.MainBusinessTableID == mainBusinessTableID);
                _synrelTableRepository.InsertRange(entities);
                SqlSugarHelper.Db.CommitTran();
            }
            catch (Exception ex)
            {
                SqlSugarHelper.Db.RollbackTran();
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }

        #endregion

    }
}
